<?php
/**
 * inserisce una relreqtype
 */
function relationshipIns(){
	global $objSDAO;
	$errMsg = "";
	//controllo input dati
	if(sxREQ('entityPar')=='')
		$errMsg .= _t("parent entity missing<br>");
	if(sxREQ('entityChi')=='')
		$errMsg .= _t("child entity missing<br>");
	if(sxREQ('number')=='')
		$errMsg .= _t("number missing");
	if( ereg("[^0-9]",sxREQ('number')))
		$errMsg .= _t("number not numeric<br>");
	if(sxREQ('descrParChi')=='')
		$errMsg .= _t("parent-child description missing<br>");
	if(sxREQ('descrChiPar')=='') 
		$errMsg .= _t("child-parent description missing<br>");
	if($errMsg != ""){return $errMsg;}
	if (sxREQ('integrityCheck') == 'N' and sxREQ('cascadeDelete') == 'Y') {
		$errMsg .= _t("cascade delete requiry ref. integrity").'<br>';
	}
	//controllo che la relreqtype non esista gi@
	$sSQL = "SELECT count(*) as numrel FROM relationship " .
			"WHERE parent_entity='".sxRAS("entitiPar")."' " .
			"AND child_entity='".sxRAS("entityChi")."' " .
			"AND number=".sxRAS("number")."";
	$objSDAO->query($sSQL,"test");
	$row = $objSDAO->fetch("test");
	if($row["numrel"] != 0) 
		$errMsg .= _t("relationship existing<br>");
	if($errMsg != ""){return $errMsg;}
	
	$sSQL = "INSERT INTO " .
			"relationship " .
			"(parent_entity, " .
			"child_entity, " .
			"number, " .
			"description_parent_child, " .
			"description_child_parent, " .
			"note, " .
			"parent_optionality," .
			"children_optionality," .
			"children_multeplicity," .
			"integrity_check," .
			"cascade_delete" .
			")" .
			"VALUES('".sxRAS("entityPar")."', " .
			"'".sxRAS("entityChi")."', " .
			sxRAS("number").", " .
			"'".sxRAS("descrParChi")."', " .
			"'".sxRAS("descrChiPar")."', " .
			"'".sxRAS("note")."', " .
			"'".sxRas('parentOptionality')."'," .
			"'".sxRas('childrenOptionality')."'," .
			"'".sxRas('childrenMulteplicity')."'," .
			"'".sxRas('integrityCheck')."'," .
			"'".sxRas('cascadeDelete')."'" .
			")";	
	$objSDAO->query($sSQL,"ins");
	$url = "../rela/relationshipMod.php?from_ins=si&da=".substr($_REQUEST["entityPar"],0,4)."&ent="
			.$_REQUEST["entityPar"]."&entityChi=".$_REQUEST["entityChi"]
			."&number=".$_REQUEST["number"]."&xsn=".XSN;
	serverRedir($url);	
	exit();
}

function relationshipUpd(){
	global $objSDAO;
	$errMsg = '';
	if(sxREQ('number')=='')
		$errMsg .= _t("Il numero; &egrave; obbligatorio").'<br>';
	if(ereg("[^0-9]",sxREQ('number')) or sxREQ('number') == 0)
		$errMsg .= _t("Il valore del numero deve essere numerico diverso da zero").'<br>';
	if(sxREQ('descrParChi') == "")
		$errMsg .= _t("La description Padre-Figlio &egrave; obbligatoria").'<br>';
	if(sxREQ('descrChiPar') == "")
		$errMsg .= _t("La description Figlio-Padre &egrave; obbligatoria").'<br>';
	foreach($_REQUEST as $nome => $val) {
		if (substr($nome,0,8)!='rel_key_') continue;
		if ($val=='') {
			$errMsg .= _t("manca associreqtype chiave int. '%s'",substr($nome,8)).'<br>';
		}
	}
	if (sxRequest('integrityCheck') == 'N' and sxRequest('cascadeDelete') == 'Y') {
		$errMsg .= _t("delete in cascata ammessa solo se test integrit@ attivo").'<br>';
	}
	if($errMsg != ""){return $errMsg;}

	if ($_REQUEST["db_number"] != $_REQUEST["number"]) {
		$sSQL = "SELECT number FROM relationship " .
				" WHERE parent_entity='".$_REQUEST["entityPar"]."' " .
				"    AND child_entity='".$_REQUEST["entityChi"]."' AND number=".$_REQUEST["number"];	
		$objSDAO->query($sSQL,"ins");
		$rec = $objSDAO->fetch('ins');
		if ($rec) {
			$errMsg .= _t("numero gia' esistente");
		}
	}
	if($errMsg != ""){return $errMsg;}
	
	$sSQL = "UPDATE relationship SET " .
			" description_parent_child='".addslashes(sxGPC($_REQUEST["descrParChi"]))."', " .
			" description_child_parent='".addslashes(sxGPC($_REQUEST["descrChiPar"]))."', " .
			" note='".addslashes(sxGPC($_REQUEST["note"]))."', " .
			" parent_optionality='".sxRAS('parentOptionality')."'," .
			" children_optionality='".sxRAS('childrenOptionality')."'," .
			" children_multeplicity='".sxRAS('childrenMulteplicity')."'," .
			" integrity_check='".sxRAS('integrityCheck')."'," .
			" cascade_delete='".sxRAS('cascadeDelete')."'" .
			" WHERE parent_entity='".$_REQUEST["ent"]."' " .
			"    AND child_entity='".$_REQUEST["entityChi"]."' " .
			"    AND number=".$_REQUEST["db_number"];	
	$objSDAO->query($sSQL,"ins");
	if ($_REQUEST["db_number"] != $_REQUEST["number"]) {
		$sSQL = "UPDATE relationship SET number = '".addslashes(sxGPC($_REQUEST["number"]))."'" .
				" WHERE parent_entity='".$_REQUEST["ent"]."' " .
				"    AND child_entity='".$_REQUEST["entityChi"]."' AND number=".$_REQUEST["db_number"];	
		$objSDAO->query($sSQL,"ins");
		$sSQL = "UPDATE relationship_key SET number = '".addslashes(sxGPC($_REQUEST["number"]))."'" .
				" WHERE parent_entity='".$_REQUEST["ent"]."' " .
				"    AND child_entity='".$_REQUEST["entityChi"]."' AND number=".$_REQUEST["db_number"];	
		$objSDAO->query($sSQL,"upd");
	}
	
	$sSQL = "DELETE FROM relationship_key " .
			" WHERE parent_entity='".$_REQUEST["ent"]."' " .
			"    AND child_entity='".$_REQUEST["entityChi"]."' AND number=".$_REQUEST["db_number"];	
	$objSDAO->query($sSQL,"upd");
	foreach($_REQUEST as $nome => $val) {
		if (substr($nome,0,8)!='rel_key_') continue;
		$sSQL = "INSERT INTO " .
			"relationship_key " .
			"(parent_entity, " .
			"child_entity, " .
			"number, " .
			"parent_attribute, " .
			"child_attribute" .
			")" .
			"VALUES('".addslashes(sxGPC($_REQUEST["ent"]))."', " .
			"'".addslashes(sxGPC($_REQUEST["entityChi"]))."', " .
			$_REQUEST["number"].", " .
			"'".addslashes(sxGPC(substr($nome,8)))."', " .
			"'".addslashes(sxGPC($val))."'" .
			")";	
		$objSDAO->query($sSQL,"upd");
	}
	logRecord("450", _t("Modificata la relreqtype")." '".$_REQUEST["number"]."' "._t("fra")." '".addslashes(sxGPC($_REQUEST["ent"]))."' "._t("e")." '".addslashes(sxGPC($_REQUEST["entityChi"]))."'");
	//serverRedir("../enty/entityView.php?da=".$_REQUEST["data_area"]."&ent=".$_REQUEST["ent"]."&xsn=".XSN);
	$url = $_SESSION[USEROBJ]->getHistory(0, true);
	serverRedir($url);
	exit();
}

function relationshipDel(){
	global $objSDAO;
	$errMsg = "";
	if(!isset($_REQUEST["ent"]) || $_REQUEST["ent"] == "")
		$errMsg .= _t("Entit&agrave; padre &egrave; obbligatoria");
	if(!isset($_REQUEST["entityChi"]) || $_REQUEST["entityChi"] == "")
		$errMsg .= _t("Entit&agrave; figlia &egrave; obbligatoria");
	
	if($errMsg != "")
		return $errMsg;
	//elimino la relation
	$sSQL = "DELETE FROM relationship " .
			"WHERE parent_entity='".$_REQUEST["ent"]."' " .
			"AND child_entity='".$_REQUEST["entityChi"]."' " .
			"AND number=".$_REQUEST["number"]."";
	if($objSDAO->query($sSQL,"test",array("contOnErr"=>true))){
		$sSQL = "DELETE FROM relationship_key " .
			"WHERE parent_entity='".$_REQUEST["ent"]."' " .
			"AND child_entity='".$_REQUEST["entityChi"]."' " .
			"AND number=".$_REQUEST["number"]."";
		if($objSDAO->query($sSQL,"test",array("contOnErr"=>true))){
			logRecord("550", _t("Eliminata la relreqtype")." '".$_REQUEST["number"]."' "._t("fra")." '".addslashes(sxGPC($_REQUEST["ent"]))."' "._t("e")." '".addslashes(sxGPC($_REQUEST["entityChi"]))."'");
			//serverRedir("../enty/entityView.php?da=".$_REQUEST["data_area"]."&ent=".$_REQUEST["ent"]."&xsn=".XSN);
			$url = $_SESSION[USEROBJ]->getHistory(0, true);
			serverRedir($url);
		}else{
		$errMsg .= $sSQL."<br> - ".$objSDAO->error;
	}
	}else{
		$errMsg .= $sSQL."<br> - ".$objSDAO->error;
	}
	return $errMsg;
}

function entSubIns(){
	global $objSDAO;
	$errMsg = "";
	if(!isset($_REQUEST["entity"]) || $_REQUEST["entity"] == "")
		$errMsg .= _t("L'entity supertipo &egrave; obbligatoria").".<br>";
	if(!isset($_REQUEST["discr_attr"]) || $_REQUEST["discr_attr"] == "")
		$errMsg .= _t("L'attribute discriminatore &egrave; obbligatorio").".<br>";
	if(!isset($_REQUEST["discr_value"]) || $_REQUEST["discr_value"] == "")
		$errMsg .= _t("Il valore discriminatore &egrave; obbligatorio").".<br>";
	if(!isset($_REQUEST["subtype_ent"]) || $_REQUEST["subtype_ent"] == "")
		$errMsg .= _t("L'entity sottotipo &egrave; obbligatoria").".<br>";
		//controllo che la relreqtype di subtype non esista 
	if($errMsg == ""){
		$sSQL = "SELECT count(*) as num FROM subtyping " .
				"WHERE supertype_entity='".addslashes(sxGPC($_REQUEST["entity"]))."' " .
				"AND discriminating_attribute='".addslashes(sxGPC($_REQUEST["discr_attr"]))."' " .
				"AND discriminating_value='".addslashes(sxGPC($_REQUEST["discr_value"]))."'";
		$num = 0;
		if($objSDAO->query($sSQL,"test",array("contOnErr"=>true))){
			if($row = $objSDAO->fetch("test")){
				$num = $row["num"]; 
			}
		}else{
			$errMsg .= $objSDAO->error;
		}
		if($num != 0)
			$errMsg .= _t("Il codice sottotipo esiste gi&agrave;");
	}
	if($errMsg == ""){
		//se supertype_entity @ gi@ inserita non la inserisco
		$sSQL = "SELECT count(*) AS num FROM subtyping WHERE supertype_entity='".addslashes(sxGPC($_REQUEST["entity"]))."'";
		$num = 0;
		if($objSDAO->query($sSQL,"test2",array("contOnErr"=>true))){
			if($row = $objSDAO->fetch("test2")){
				$num = $row["num"]; 
			}
		}else{
			$errMsg .= $objSDAO->error;
		}
		if($num == 0 && $errMsg == ""){
			$sSQL = "INSERT INTO subtyping (supertype_entity, discriminating_attribute, description, note)" .
					"VALUES(" .
					"'".addslashes(sxGPC($_REQUEST["entity"]))."', " .
					"'".addslashes(sxGPC($_REQUEST["discr_attr"]))."', " .
					"'".addslashes(sxGPC($_REQUEST["description"]))."', " .
					"'".addslashes(sxGPC($_REQUEST["note"]))."'" .
					")";;
			if(!$objSDAO->query($sSQL,"ins",array("contOnErr"=>true))){
				$errMsg .= $sSQL."<br> - ".$objSDAO->error;
			}
		}
		if($errMsg == ""){
			$sSQL = "INSERT INTO subtyping_code (supertype_entity, " .
					"discriminating_attribute, " .
					"discriminating_value, " .
					"subtype_entity)" .
					"VALUES(" .
					"'".addslashes(sxGPC($_REQUEST["entity"]))."', " .
					"'".addslashes(sxGPC($_REQUEST["discr_attr"]))."', " .
					"'".addslashes(sxGPC($_REQUEST["discr_value"]))."', " .
					"'".addslashes(sxGPC($_REQUEST["subtype_ent"]))."'" .
					")";
			//echo($sSQL);
			if(!$objSDAO->query($sSQL,"ins",array("contOnErr"=>true))){
				$errMsg .= $sSQL."<br> - ".$objSDAO->error;
			}else{
				//serverRedir("../enty/entityView.php?da=".$_REQUEST["data_area"]."&ent=".$_REQUEST["entity"]."&xsn=".XSN);
				$url = $_SESSION[USEROBJ]->getHistory(-1, true);
				serverRedir($url);
			}
		}
	}
	return $errMsg;
}


function entSubUpd(){
	global $objSDAO;
	$errMsg = "";
	if(!isset($_REQUEST["entity"]) || $_REQUEST["entity"] == "")
		$errMsg .= _t("L'entity supertipo &egrave; obbligatoria").".<br>";
	if(!isset($_REQUEST["discr_attr"]) || $_REQUEST["discr_attr"] == "")
		$errMsg .= _t("L'attribute discriminatore &egrave; obbligatorio").".<br>";
	if(!isset($_REQUEST["discr_value"]) || $_REQUEST["discr_value"] == "")
		$errMsg .= _t("Il valore discriminatore &egrave; obbligatorio").".<br>";
	if(!isset($_REQUEST["subtype_ent"]) || $_REQUEST["subtype_ent"] == "")
		$errMsg .= _t("L'entity sottotipo &egrave; obbligatoria").".<br>";
	
	if($errMsg == ""){
		$sSQL = "UPDATE subtyping SET " .
				"description='".addslashes(sxGPC($_REQUEST["description"]))."', " .
				"note='".addslashes(sxGPC($_REQUEST["note"]))."' " .
				"WHERE supertype_entity='".addslashes(sxGPC($_REQUEST["entity"]))."' AND discriminating_attribute='".addslashes(sxGPC($_REQUEST["discr_attr"]))."'";
		if(!$objSDAO->query($sSQL,"ins",array("contOnErr"=>true))){
			$errMsg .= $sSQL."<br> - ".$objSDAO->error;
		}else{
			$sSQL = "UPDATE subtyping_code SET " .
					"discriminating_attribute='".addslashes(sxGPC($_REQUEST["discr_attr"]))."', " .
					"discriminating_value='".addslashes(sxGPC($_REQUEST["discr_value"]))."' " .
					"WHERE supertype_entity='".addslashes(sxGPC($_REQUEST["entity"]))."' AND subtype_entity='".addslashes(sxGPC($_REQUEST["subtype_ent"]))."'";
			//echo($sSQL);
			if(!$objSDAO->query($sSQL,"ins",array("contOnErr"=>true))){
				$errMsg .= $sSQL."<br> - ".$objSDAO->error;
			}else{
				//serverRedir("../enty/entityView.php?da=".$_REQUEST["data_area"]."&ent=".$_REQUEST["entity"]."&xsn=".XSN);
				$url = $_SESSION[USEROBJ]->getHistory(0, true);
				serverRedir($url);
			}
		}
	}
	return $errMsg;
}

function entSubDel(){
	global $objSDAO;
	$errMsg = "";
	if(!isset($_REQUEST["entity"]) || $_REQUEST["entity"] == "")
		$errMsg .= _t("L'entity supertipo &egrave; obbligatoria").".<br>";
	if(!isset($_REQUEST["discr_attr"]) || $_REQUEST["discr_attr"] == "")
		$errMsg .= _t("L'attribute discriminatore &egrave; obbligatorio").".<br>";
	if(!isset($_REQUEST["discr_value"]) || $_REQUEST["discr_value"] == "")
		$errMsg .= _t("Il valore discriminatore &egrave; obbligatorio").".<br>";
	if(!isset($_REQUEST["subtype_ent"]) || $_REQUEST["subtype_ent"] == "")
		$errMsg .= _t("L'entity sottotipo &egrave; obbligatoria").".<br>";
	
	if($errMsg == ""){
		$sSQL = "DELETE FROM subtyping " .
				"WHERE supertype_entity='".addslashes(sxGPC($_REQUEST["entity"]))."'";
		if(!$objSDAO->query($sSQL,"ins",array("contOnErr"=>true))){
			$errMsg .= $sSQL."<br> - ".$objSDAO->error;
		}else{
			$sSQL = "DELETE FROM subtyping_code " .
					"WHERE supertype_entity='".addslashes(sxGPC($_REQUEST["entity"]))."' AND subtype_entity='".addslashes(sxGPC($_REQUEST["subtype_ent"]))."'";
			//echo($sSQL);
			if(!$objSDAO->query($sSQL,"ins",array("contOnErr"=>true))){
				$errMsg .= $sSQL."<br> - ".$objSDAO->error;
			}else{
				//serverRedir("../enty/entityView.php?da=".$_REQUEST["data_area"]."&ent=".$_REQUEST["entity"]."&xsn=".XSN);
				$url = $_SESSION[USEROBJ]->getHistory(0, true);
				serverRedir($url);
			}
		}
	}
	return $errMsg;
}

function checkInput(){
	$errMsg = "";
	return $errMsg;
}
?>
